package com.cy.ppj.service.sys.impl;

import com.cy.ppj.comm.constant.CacheConstants;
import com.cy.ppj.comm.utils.JsonObjectMapper;
import com.cy.ppj.dao.mapper.sys.SystemSettingsMapper;
import com.cy.ppj.model.ao.admin.BrokerCommissionSettingsAO;
import com.cy.ppj.model.domian.admin.SystemSettings;
import com.cy.ppj.model.dto.sys.BrokerCommissionSettingsDTO;
import com.cy.ppj.model.dto.sys.TeamCommissionSettingsDTO;
import com.cy.ppj.model.enums.admin.SystemSettingType;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.sys.SystemSettingsService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @description: 系统设置
 * @author: Ocean
 * @create: 2018-09-05 14:38
 **/
@Data
@Service
public class SystemSettingsServiceImpl extends BaseService implements SystemSettingsService {

    @Autowired
    private SystemSettingsMapper systemSettingsMapper;

    private final String TEAM_FIRST_RATE = "TEAM_FIRST_RATE";

    private final String TEAM_SECOND_RATE = "TEAM_SECOND_RATE";

    private final String BROKER_OFFICIAL = "BROKER_OFFICIAL";

    private final String BROKER_GOLDEN = "BROKER_GOLDEN";

    private final String BROKER_DIAMOND = "BROKER_DIAMOND";

    @Override
    @Cacheable(value = CacheConstants.CACHE_SYSTEM_SETTINGS)
    public TeamCommissionSettingsDTO queryTeamCommissionSettingsInCache() {

        TeamCommissionSettingsDTO teamCommissionSettings = new TeamCommissionSettingsDTO();

        SystemSettings systemSettings = new SystemSettings();
        systemSettings.setType(SystemSettingType.TEAM_COMMISSION);
        List<SystemSettings> systemSettingsList = systemSettingsMapper.select(systemSettings);

        systemSettingsList.stream().forEach(s -> {
            switch (s.getName()) {
                case TEAM_FIRST_RATE:
                    teamCommissionSettings.setFirstRate(new BigDecimal(s.getValue()));
                    teamCommissionSettings.setFirstSettingId(s.getId());
                    teamCommissionSettings.setFirstUpdateTime(s.getUpdateTime());
                    break;
                case TEAM_SECOND_RATE:
                    teamCommissionSettings.setSecondRate(new BigDecimal(s.getValue()));
                    teamCommissionSettings.setSecondSettingId(s.getId());
                    teamCommissionSettings.setSecondUpdateTime(s.getUpdateTime());
                    break;
                default:
                    break;
            }
        });
        return teamCommissionSettings;
    }

    @Override
    @Cacheable(value = CacheConstants.CACHE_SYSTEM_SETTINGS)
    public BrokerCommissionSettingsDTO queryBrokerCommissionSettingsInCache() {

        BrokerCommissionSettingsDTO brokerCommissionSettings = new BrokerCommissionSettingsDTO();

        SystemSettings systemSettings = new SystemSettings();
        systemSettings.setType(SystemSettingType.BROKER);
        List<SystemSettings> systemSettingsList = systemSettingsMapper.select(systemSettings);

        systemSettingsList.stream().forEach(s -> {

            BrokerCommissionSettingsAO brokerCommissionSettingsAO = JsonObjectMapper.read(s.getValue(), BrokerCommissionSettingsAO.class);
            BigDecimal upgradeCondition = brokerCommissionSettingsAO.getUpgradeCondition();
            BigDecimal keepCondition = brokerCommissionSettingsAO.getKeepCondition();
            BigDecimal rate = brokerCommissionSettingsAO.getRate();

            switch (s.getName()) {
                case BROKER_OFFICIAL:
                    brokerCommissionSettings.setOfficalId(s.getId());
                    brokerCommissionSettings.setOfficalKeepCondition(keepCondition);
                    brokerCommissionSettings.setOfficalUpgradeCondition(upgradeCondition);
                    brokerCommissionSettings.setOfficalRate(rate);
                    brokerCommissionSettings.setOfficalUpdateTime(s.getUpdateTime());
                    break;
                case BROKER_GOLDEN:
                    brokerCommissionSettings.setGoldenId(s.getId());
                    brokerCommissionSettings.setGoldenKeepCondition(keepCondition);
                    brokerCommissionSettings.setGoldenUpgradeCondition(upgradeCondition);
                    brokerCommissionSettings.setGoldenRate(rate);
                    brokerCommissionSettings.setGoldenUpdateTime(s.getUpdateTime());
                    break;
                case BROKER_DIAMOND:
                    brokerCommissionSettings.setDiamondId(s.getId());
                    brokerCommissionSettings.setDiamondKeepCondition(keepCondition);
                    brokerCommissionSettings.setDiamondUpgradeCondition(upgradeCondition);
                    brokerCommissionSettings.setDiamondRate(rate);
                    brokerCommissionSettings.setDiamondUpdateTime(s.getUpdateTime());
                    break;
                default:
                    break;
            }
        });

        return brokerCommissionSettings;
    }

    @Override
    @Transactional
    @CacheEvict(value = CacheConstants.CACHE_SYSTEM_SETTINGS, allEntries = true, beforeInvocation = true)
    public void updateSystemSettings(SystemSettings systemSettings) {
        systemSettingsMapper.updateByPrimaryKeySelective(systemSettings);
    }
}
